package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.MouseUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

/**
 * 刮刮乐任务
 */
@Slf4j
public class ScratchTicketTaskImpl extends AbstractTask {

    public ScratchTicketTaskImpl(WinDef.HWND hwnd_0) {
        super(hwnd_0);
    }

    @Override
    public String getTaskName() throws Exception {
        return "刮刮乐";
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc3();
        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_福利, 2).isFinded()) {
            return true;
        }
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {
        // 找到福利并打开
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_福利)) {
            return false;
        }

        // 找到每日福利并打开
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_0, PicConstants.刮刮乐任务_每日福利)) {
            return false;
        }

        // 检查完成状态
        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.刮刮乐任务_已完成, 3).isFinded()) {
            clickEsc1();
            return null;
        }

        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.刮刮乐任务_打开)) {
            return false;
        }

        ThreadUtil.sleep1();
        log.info("【刮刮乐任务】开始刮奖！");

        MouseUtil.dragMove(hwnd_0, 390, 260, 250, 0);
        MouseUtil.dragMove(hwnd_0, 390, 275, 250, 0);
        MouseUtil.dragMove(hwnd_0, 390, 290, 250, 0);
        MouseUtil.dragMove(hwnd_0, 390, 305, 250, 0);
        MouseUtil.dragMove(hwnd_0, 390, 320, 250, 0);
        MouseUtil.dragMove(hwnd_0, 390, 335, 250, 0);
        MouseUtil.dragMove(hwnd_0, 390, 350, 250, 0);
        // 确认是否刮开
        if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.刮刮乐任务_刮完了, 3).isFinded()) {
            MouseUtil.dragMove(hwnd_0, 390, 260, 250, 0);
            MouseUtil.dragMove(hwnd_0, 390, 275, 250, 0);
            MouseUtil.dragMove(hwnd_0, 390, 290, 250, 0);
            MouseUtil.dragMove(hwnd_0, 390, 305, 250, 0);
            MouseUtil.dragMove(hwnd_0, 390, 320, 250, 0);
            MouseUtil.dragMove(hwnd_0, 390, 335, 250, 0);
            MouseUtil.dragMove(hwnd_0, 390, 350, 250, 0);
        }

        return true;
    }

    @Override
    public Boolean after() throws Exception {
        ThreadUtil.sleep2();
        if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.刮刮乐任务_刮完了, 3).isFinded()) {
            return false;
        }
        clickEsc3();
        return true;
    }
}
